package api

import (
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"go.uber.org/zap"
	"net/http"
	"web_end/global/response"
)

var store = base64Captcha.DefaultMemStore

// GetCaptcha /* 生成图形验证码
func GetCaptcha(ctx *gin.Context) {
	driver := base64Captcha.NewDriverDigit(80, 240, 5, 0.7, 80)
	cp := base64Captcha.NewCaptcha(driver, store)
	id, base64, err := cp.Generate()
	if err != nil {
		zap.S().Errorf("生成验证码错误: %s", err.Error())
		ctx.JSON(http.StatusOK, gin.H{
			"code": response.CodeInternalServerError,
			"msg":  "生成验证码错误",
		})
		return
	}
	data := map[string]interface{}{
		"captchaId": id,
		"picPath":   base64,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
		"data": data,
	})
}
